
/**
 * 设置 vxe-table 拖动事件
 * vxe-table 需开启 row-key
 */
import Sortable from 'sortablejs'
export default {
  methods: {
    /**
     * 交换 数组元素位置
     * @param {*} arr
     * @param {*} newIndex
     * @param {*} oldIndex
     */
    swapArray (arr, newIndex, oldIndex) {
      arr[newIndex] = arr.splice(oldIndex, 1, arr[newIndex])[0]
      return arr
    },
    /**
     * 行拖动
     */
    rowDrop (xTable) {
      if (!xTable) {
        xTable = this.$refs.editable
      }
      this.$nextTick(() => {
        this.uniedpSortable = Sortable.create(xTable.$el.querySelector('.body--wrapper>.vxe-table--body tbody'), {
          handle: '.drag-btn',
          onEnd: ({ newIndex, oldIndex }) => {
            let data = xTable.getTableData().tableData
            this.dataList = this.swapArray(data, newIndex, oldIndex)
          }
        })
      })
    }
  }
}
